package TXPathFinder;

import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.picking.PickTool;
import com.sun.j3d.utils.picking.behaviors.PickMouseBehavior;
import java.awt.event.MouseEvent;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import javax.media.j3d.Appearance;
import javax.media.j3d.Bounds;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
import javax.vecmath.Color3f;

/**
*       \class	PickHighlightBloc
*	\brief	Gère le highLight des blocs (la séléction sur la map3D de bloc pour placer ou deplacer un avatar)
**/
class PickHighlightBloc extends PickMouseBehavior
{

    private Appearance savedAppearance = null;
    private Shape3D oldShape = null;
    private Viewer view;
    private boolean isSphere;
    private Appearance highlightAppearance;
    private Appearance highlightAppearanceRed;

    /**
    *	\brief	Constructeur
    *   \param canvas Le canvas3D ou est stocker la viewer3D
    *   \param root La racine des objets 3D
    *   \param bounds Cercle definissant l'espace de la selection
    *   \param view Le viewer3D
    **/
    public PickHighlightBloc(Canvas3D canvas, BranchGroup root, Bounds bounds, Viewer view)
    {
        super(canvas, root, bounds);
        this.view = view;
        isSphere = false;
        this.setSchedulingBounds(bounds);
        root.addChild(this);
        Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
        Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
        Color3f highlightColor = new Color3f(0.2f, 1.0f, 1.0f);
        highlightAppearance = new Appearance();
        highlightAppearance.setMaterial(new Material(highlightColor, black,
            highlightColor, white, 20.0f));

        Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
        Color3f highlightColorPath = new Color3f(0.0f, 0.0f, 0.0f);
        highlightAppearanceRed = new Appearance();
        highlightAppearanceRed.setMaterial(new Material(highlightColorPath, red,
            highlightColorPath, white, 250.0f));

        pickCanvas.setMode(PickTool.BOUNDS);
    }

    /**
    *	\brief	Changement de couleur sur la forme sélectionnée
    *   \param xpos Position x de la sélection
    *   \param ypos Position y de la sélection
    **/
    public void updateScene(int xpos, int ypos)
    {
        PickResult pickResult = null;
        Shape3D shape = null;
        pickCanvas.setShapeLocation(xpos, ypos);

        pickResult = pickCanvas.pickClosest();
        if (pickResult != null) {
            shape = (Shape3D) pickResult.getNode(PickResult.SHAPE3D);
            DataModel.getInstance().setFocusTaken(true);
        }
        else
            DataModel.getInstance().setFocusTaken(false);

        if (oldShape != null) {
            oldShape.setAppearance(savedAppearance);
        }
        if (shape != null)
        {
            String str = shape.getName();
            int i = str.lastIndexOf(':');
            int xx = Integer.parseInt(str.substring(0,i));
            int yy = Integer.parseInt(str.substring(i+1));

            if(this.mevent.getButton() == MouseEvent.BUTTON1)
            {
                DataModel.getInstance().setCoordHighlight(xx, yy);
       
                Set<String> set = DataModel.getInstance().listAvatar.keySet();
                Iterator iterator = set.iterator();
                while ( iterator.hasNext())
                {
                    Object keys = iterator.next();
                    if( ((int)((Avatar)DataModel.getInstance().listAvatar.get((Integer)keys)).xPos == xx) && (((int)ShapeService.getLargeur() - (int)((Avatar)DataModel.getInstance().listAvatar.get((Integer)keys)).yPos - 1)  == yy) )
                    {
                        isSphere = true;
                    }
                }

                if( isSphere == false )
                {
                    savedAppearance = shape.getAppearance();
                    oldShape = shape;
                    shape.setAppearance(highlightAppearance);
                    DataModel.getInstance().setIsAvatarSelected(false);
                    DataModel.getInstance().setAvatarCurrent(-1);
                    view.setCoordToSpinner(xx,yy);
                }
                isSphere = false;
            }
            else if (this.mevent.getButton() == MouseEvent.BUTTON3)
            {
                if( DataModel.getInstance().isIsAvatarSelected() )
                {
                    savedAppearance = shape.getAppearance();
                    oldShape = shape;
                    shape.setAppearance(highlightAppearanceRed);
                    DataModel.getInstance().setGotoAgent(xx,yy);
                }
            }
        }
    }
}
